package org.example.afree;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class Roman_numerals_to_whole_numbers {
    public static HashMap<Character, Integer> map = new HashMap<>();
    static {
        map.put('I',1);
        map.put('V',5);
        map.put('X',10);
        map.put('L',50);
        map.put('C',100);
        map.put('D',500);
        map.put('M',1000);
    }
    public static void main(String[] args) {
    //I             1
    //V             5
    //X             10
    //L             50
    //C             100
    //D             500
    //M             1000
        String s = "MCMXCIV";
        System.out.println(romanToInt(s));
    }

    public static int romanToInt(String s) {
        List<Character> list = s.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
        int n = 0;
        int sum = 0;
        while(n< list.size()){
            if (n != list.size()-1 && list.get(n) == 'I' && list.get(n+1) == 'V'){
                sum+=4;
                n = n+2;
            }else if (n != list.size()-1 && list.get(n) == 'I' && list.get(n+1) == 'X'){
                sum+=9;
                n = n+2;
            }else if (n != list.size()-1 && list.get(n) == 'X' && list.get(n+1) == 'L'){
                sum+=40;
                n = n+2;
            }else if(n != list.size()-1 && list.get(n) == 'X' && list.get(n+1) == 'C'){
                sum+=90;
                n = n+2;
            }else if (n != list.size()-1 && list.get(n) == 'C' && list.get(n+1) == 'D'){
                sum+=400;
                n = n+2;
            }else if (n != list.size()-1 && list.get(n) == 'C' && list.get(n+1) == 'M'){
                sum+=900;
                n = n+2;
            }else {
                sum+=map.get(list.get(n));
                n++;
            }

        }
        return sum;
    }
}
